using SG3L_RAG.Application.DTOs;
using System.Text.Json;

// 测试向量搜索的诊断脚本
public class VectorSearchDiagnostics
{
    public static async Task DiagnoseVectorSearch()
    {
        // 测试不同的阈值
        var thresholds = new[] { 0.1f, 0.3f, 0.5f, 0.7f, 0.9f };
        var query = "什么是RAG技术";
        
        Console.WriteLine($"诊断查询: {query}");
        Console.WriteLine("=" + new string('=', 50));
        
        foreach (var threshold in thresholds)
        {
            Console.WriteLine($"\n阈值: {threshold}");
            
            var request = new VectorSearchRequestDto
            {
                Query = query,
                TopK = 10,
                Threshold = threshold
            };
            
            var body = JsonSerializer.Serialize(request);
            
            try
            {
                using var client = new HttpClient();
                var response = await client.PostAsync(
                    "http://localhost:5000/api/VectorSearch/chunks",
                    new StringContent(body, System.Text.Encoding.UTF8, "application/json"));
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    var results = JsonSerializer.Deserialize<List<DocumentChunkSearchResultDto>>(content);
                    
                    Console.WriteLine($"找到 {results?.Count ?? 0} 个结果");
                    
                    if (results?.Any() == true)
                    {
                        foreach (var result in results.Take(3))
                        {
                            Console.WriteLine($"  - 相似度: {result.Score:F4}, 内容: {result.Content.Substring(0, Math.Min(50, result.Content.Length))}...");
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"请求失败: {response.StatusCode}");
                    var error = await response.Content.ReadAsStringAsync();
                    Console.WriteLine($"错误: {error}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异常: {ex.Message}");
            }
        }
    }
}
